package com.init.engineering.dao;


import com.init.engineering.pages.QueryPageBean;
import com.init.engineering.pojo.Item;
import com.init.engineering.pojo.Subitem;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface ItemDao {

    @Results(id = "ItemMap",value = {
            @Result(column = "suid",property = "suid"),
            @Result(column = "suid",property = "subitem",javaType = Subitem.class,one =
            @One(select = "com.init.engineering.dao.SubitemDao.findSubitemBySuid",fetchType = FetchType.EAGER))
    })
    @Select("SELECT * from item where status = 1")
    List<Item> findItems();


    @ResultMap("ItemMap")
    @Select({"<script> SELECT * FROM item <where>" +
            "<if test=\"queryString!=null and queryString != ''\">" +
            "and iname like concat('%',#{queryString},'%')" +
            "</if>" +
            "and status=1" +
            "</where> limit #{currentPage},#{pageSize}" +
            "</script>"})
    List<Item> finditemsByPage(QueryPageBean queryPageBean);


    @Select({"<script> SELECT count(*) FROM item <where>" +
            "<if test=\"queryString!=null and queryString != ''\">" +
            "and iname like concat('%',#{queryString},'%')" +
            "</if>" +
            "and status=1" +
            "</where>" +
            "</script>"})
    long selectTotalCount(@Param("queryString")String queryString);



    @Update("UPDATE item set status = 0 where iid = #{iid}")
    int updateItemStatus(@Param("iid")String iid);


    @Update("<script>update item <set><if test='iname != null'>iname = #{iname},</if>" +
            "<if test='suid != null'>suid = #{suid},</if>" +
            "<if test='starttime != null'>starttime = #{starttime},</if>" +
            "<if test='endtime != null'>endtime = #{endtime},</if>" +
            "<if test='updatetime != null'>updatetime = #{updatetime}</if>" +
            "</set> where iid = #{iid}</script>")
    int updateItem(Item Item);


    @Insert("insert into item(iid,iname,suid,starttime,endtime,updatetime,status)" +
            " values(#{iid},#{iname},#{suid},#{starttime},#{endtime},#{updatetime},1)")
    int saveItem(Item item);

    /**
     * 找到分项工程对应的所有分项子工程
     * @param suid
     * @return
     */
    @Select("select * from item where suid = #{suid} and status = 1")
    List<Item> findSubitemBySuid(String suid);


    @Select("select * from item where iid = #{iid} and status = 1")
    Item findItemByIid(String Iid);
}
